package singleton;

/**
 * 饿汉式
 * 双重检查机制
 * @author wangxun
 * @version 1.0
 * @description:
 * @date 2020/11/13 9:19 上午
 */
public class HungrySingleton2 {
    /**
     *  防止指令重排 线程可见性 不经过缓存
      */
    private static volatile HungrySingleton2 hungrySingleton;

    private HungrySingleton2(){

    }


    public static HungrySingleton2 getSingletonInstance(){
        if(hungrySingleton != null){
            // 加锁
            synchronized (HungrySingleton2.class){
               // 获取锁 之后再判断是否已经创建好对象了
                if(hungrySingleton!=null){
                    hungrySingleton= new HungrySingleton2();
                }
            }
        }
        return hungrySingleton;
    }

}
